<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/12/16
 * Time: 17:05
 */
require 'config.php';
switch (@$_GET['a']){
    case 'login':
        if(!empty($_POST['uname']) && !empty($_POST['pwd'])){
            $uname = $_POST['uname'];
            $pwd = $_POST['pwd'];
            @$ji = $_POST['ji'];
            $sql = "select * from user where userName='{$uname}' and password='{$pwd}' and status=1 and auth <2";
            $result = mysqli_query($link,$sql);
            $rows = mysqli_fetch_assoc($result);
            $id = $rows['id'];
            if ($rows){
                if ($rows['auth'] ==1 ){
                    if ($ji == 1){
                        setcookie('auth1','会员用户',time()+3600,'/');
                        setcookie('uname1',$uname,time()+3600*24,'/');
                        setcookie('id1',$id,time()+3600*24,'/');
                        echo "<script>alert('登录成功');window.location.href='index.php';</script>";die;
                    }else{
                        setcookie('auth1','会员用户',0,'/');
                        setcookie('uname1',$uname,0,'/');
                        setcookie('id1',$id,0,'/');
                        echo "<script>alert('登录成功');window.location.href='index.php';</script>";die;
                    }

                }elseif ($rows['auth'] ==0) {
                    if ($ji == 1){
                        setcookie('auth1','普通用户',time()+3600*24,'/');
                        setcookie('uname1',$uname,time()+3600,'/');
                        setcookie('id1',$id,time()+3600*24,'/');
                        echo "<script>alert('登录成功');window.location.href='index.php';</script>";die;
                    }else{
                        setcookie('auth1','普通用户',0,'/');
                        setcookie('uname1',$uname,0,'/');
                        setcookie('id1',$id,0,'/');
                        echo "<script>alert('登录成功');window.location.href='index.php';</script>";die;
                    }

                }
            }else{
                echo "<script>alert('用户名不存在或用户名被封');window.location.href='index.php';</script>";die;
            }
        }else{
            echo "<script>alert('内容不能为空');window.location.href='index.php';</script>";die;
        }
        break;
    case 'exit':
        $time = date('Y-m-d H:i:s');
        $uname = $_COOKIE['uname1'];
        $sql ="update user set lastlogin = '{$time}' where userName = '{$uname}'";
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)>0){
            setcookie('uname1','',time()-1,'/');
            setcookie('auth1','',time()-1,'/');
            setcookie('id1','',time()-1,'/');
            echo "<script>alert('退出成功');window.location.href='../index.php';</script>";die;
        }
        mysqli_close($link);
        break;


}